#include<stdio.h>
#include<stdlib.h>

int *array;
int size=0,i=0;
int b;

print()
{
	for(i=0;i<size;i++)
{
	printf(" %d ",array[i]);
}
	printf("\n");
}


append()
{

        int num;
        array=realloc(array,sizeof(int)*size+1);
        printf("enter a number");
        scanf("%d",&num);
        array[size]=num;
        size++;
	print();
	
}

count()
{
	int countnum=0;
	printf("enter the number whose occurence you want to check\n");
	scanf("%d",&b);
	for(i=0;i<=size;i++)
{
	if(array[i]==b)
{
	countnum++;
}
}
	printf("the no of elements in array are %d\n",countnum);
	
}

extend()
{
	int a;
	printf("enter the no of elements you want to enter");
	scanf("%d",&a);
	int arr[a]; 
	for(i=1;i<=a;i++)
{
	printf("enter the element you want to enter");
	scanf("%d",&arr[i]);
}
	for(i=1;i<=a;i++)
{
	array=realloc(array,sizeof(int)*size+1);
	array[size]=arr[i];
	size++;
}
	print();
	
}

insert()
{
	int b,d,temp,temp1;
	printf("enter the index in array\n");
	scanf("%d",&d);
	if(d>size)
{
	printf("invalid input\n");
}
	else
{
	printf("enter a number\n");
	scanf("%d",&b);
	
	array=realloc(array,sizeof(int)*(size+1));
	size++;

	for(i=d;i<=size;i++)
{
	temp=array[i]; 
	array[i]=b;
	b=temp;
}			
	print();
	
}
}	

delete()
{
	int i,d,c,temp;
	printf("enter a number to be removed");
	scanf("%d",&c);

	for(i=0;i<=size;i++)
{
	if(array[i]==c)
{
		temp=i;
		break;
}
}
	for(d=i;d<=size;d++)
	{
			array[d]=array[d+1];
	}		
	size=size-1;
	print();
	
}

pop()
{
	int i,d,c,temp;
	printf("enter the index");
	scanf("%d",&c);

	temp=array[c];
	printf("the deleted number is %d\n",temp);

	for(d=c;d<=size;d++)
	{
			array[d]=array[d+1];
	}		
	size=size-1;

	print();
	
}

reverse()
{
	int j,z;
	i=i-1;
	j=size+1;

	while(i<j)
{
	i++;
	j--;
	
	z=array[i];
	array[i]=array[j];
	array[j]=z;
}
	
	print();
}

sort()
{
	int j,z;

	for(i=0;i<=size-2;i++)
{
	for(j=i+1;j<=size-1;j++)
		{
			if(array[i]>array[j])
			{
			z=array[i];
			array[i]=array[j];
			array[j]=z;
			}
		}
}
	print();
}


main()
{
      	
      int n,p;
	
      for(;;)
{
      printf("enter the operation on array\n");
	printf("1.Append\n");
	printf("2.Count\n");
	printf("3.Extend\n");
	printf("4.Insert\n");
	printf("5.Delete\n");
	printf("6.Pop\n");
	printf("7.Reverse\n");
	printf("8.Sort\n");
        printf("9.Exit\n");
		
      scanf("%d",&n);            
     switch(n)
      {
               case(1):
                       append();
                       break;
               case(2):
                       count();
                       break;
		case(3):
			extend();
			break;
		case(4):
			insert();
			break;
		case(5):
			delete();
			break;
		case(6):
			pop();
			break;
		case(7):
			reverse();
			break;
		case(8):
			sort();
			break;
 	       case(9):
			exit(0);
}
}
return 0;
}
